package gw.com.android.ui.onlive.analyst;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * 分析师页面的标签的背景
 *
 * @author jett
 * @since 2018-06-06.
 */
public class TabCornorDrawable extends Drawable {

    private final Paint mPaint;
    private final RectF mRect;
    private final int mRadius;
    private float mPercent = 1;

    TabCornorDrawable(int radius) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xFFFFFFFF);
        mRect = new RectF();
        this.mRadius = radius;
    }

    public void setRadiusPercent(float percent) {
        this.mPercent = percent;
        invalidateSelf();
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        Rect rect = getBounds();
        int radius = (int) (mRadius * mPercent);
        if (radius >= rect.bottom / 2) {
            radius = rect.bottom / 2;
        }
        mRect.set(rect.left, rect.top, rect.right, rect.bottom);
        canvas.drawRoundRect(mRect, radius, radius, mPaint);
        mRect.set(rect.left, rect.bottom - radius, rect.right, rect.bottom);
        canvas.drawRect(mRect, mPaint);
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

}
